package com.example.controller;


import com.example.config.R;
import com.example.domain.Account;
import com.example.param.ChangePasswordParam;
import com.example.param.DistributeParam;
import com.example.param.InsertParam;
import com.example.param.LoginParam;
import com.example.service.IAccountService;
import com.example.service.IPowerService;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Delete;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 进销商账户Controller
 *
 * @author ruoyi
 * @date 2022-11-22
 */
@RestController
@RequestMapping("/system/account")
public class AccountController {

    @Autowired
    private IAccountService iAccountService;

    private IPowerService iPowerService;

    /**
     * 登录
     *
     * @param loginParam
     * @param result
     * @param request
     * @param response
     * @return
     */
    @PostMapping("login")
    public R login(@RequestBody @Validated LoginParam loginParam, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
        if (result.hasErrors()) {
            return R.fail("参数错误，请重新输入");
        }
        R login = iAccountService.login(loginParam);
        Account data = (Account) login.getData();
        Cookie[] account = new Cookie[3];
        account[0] = new Cookie("dealerId", data.getDealerId().toString());
        account[1] = new Cookie("password", data.getPassword());
        account[2] = new Cookie("username", data.getUserName());
        response.addCookie(account[0]);
        response.addCookie(account[1]);
        response.addCookie(account[2]);
        HttpSession session = request.getSession();
        System.out.println(session.getAttribute("account") + "aaaaaaa");
        session.setAttribute("account", data);
        return login;
    }

    /**
     * 修改原密码
     *
     * @param changePasswordParam
     * @param result
     * @param request
     * @return
     */
    @PostMapping("update")
    public R changePassword(@RequestBody @Validated ChangePasswordParam changePasswordParam, BindingResult result, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Account account = (Account) session.getAttribute("account");
        Long dealerId = account.getDealerId();
        changePasswordParam.setDealerId(dealerId);
        if (result.hasErrors()) {
            return R.fail("参数错误，请重新输入");
        }
        return iAccountService.changePassword(changePasswordParam);
    }

    /**
     * 添加子账户
     *
     * @param insertParam
     * @param result
     * @return
     */
    @PostMapping("insert")
    public R insert(@RequestBody @Validated InsertParam insertParam, BindingResult result) {
        if (result.hasErrors()) {
            return R.fail("请检查参数是否正确!");
        }
        return iAccountService.insert(insertParam);
    }

    /**
     * 检查用户名是否存在
     *
     * @param account
     * @return
     */
    @PostMapping("checkUserName")
    public R checkUserName(@RequestBody Account account) {
        String userName = account.getUserName();
        if (userName == null || userName.length() == 0) {
            return R.fail("用户名输入错误,请重新输入!");
        }
        return iAccountService.checkUserName(userName);
    }

    /**
     * 根据条件查询账户信息
     *
     * @param account
     * @return
     */
    @PostMapping("query")
    public R query(@RequestBody Account account) {
        return iAccountService.query(account);
    }

    /**
     * 分配权限
     *
     * @param distributeParam
     * @param result
     * @return
     */
    @PostMapping("distribute")
    public R distribute(@RequestBody @Validated DistributeParam distributeParam, BindingResult result) {
        if (result.hasErrors()) {
            return R.fail("参数错误!");
        }
        return iPowerService.distribute(distributeParam);
    }

    /**
     * 查询有哪些权限
     *
     * @param dealerId
     * @return
     */
    @GetMapping("getPower")
    public R getPower(@PathVariable("dealerId") Long dealerId) {
        if (dealerId == null) {
            return R.fail("参数错误");
        }
        return iPowerService.getPower(dealerId);
    }

    /**
     * 禁用
     *
     * @param ids
     * @return
     */
    @PostMapping("disable")
    public R disable(@RequestParam List<Integer> ids) {
        if (ids == null || ids.size() == 0) {
            return R.fail("参数错误");
        }
        return iAccountService.disable(ids);
    }

    /**
     * 启用
     *
     * @param ids
     * @return
     */
    @PostMapping("able")
    public R able(@RequestParam List<Integer> ids) {
        if (ids == null || ids.size() == 0) {
            return R.fail("参数错误");
        }
        return iAccountService.able(ids);
    }

    /**
     * 删除子账户
     *
     * @param ids
     * @return
     */
    @PostMapping("deleteById")
    public R deleteById(@RequestParam List<Integer> ids) {
        if (ids == null || ids.size() == 0) {
            return R.fail("参数错误");
        }
        return iAccountService.deleteById(ids);
    }

    /**
     * 获取账户信息
     *
     * @param dealerId
     * @return
     */
    @PostMapping("getDetail")
    public R getDetail(Long dealerId) {

        if (dealerId == null) {
            return R.fail("参数错误");
        }
        return iAccountService.getDetail(dealerId);
    }
}
